// pages/personal-profit/personal-profit.js

import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userProfit
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: "",
    uid: "", 
    balance: 0,
    teamNum: 0,
    isRequest: false,
    searchGood: {
    	page: 1,
    	size: 10,
      pages: 0
    },
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取收益金额
	var userData=wx.getStorageSync('Info')
	this.setData({
		userData:userData
	})
	// console.log(wx.getStorageSync('userInfo').uid,"wx.getStorageSync('userInfo').uid")
    api.post(userProfit.profit, {
      uid:this.data.uid||wx.getStorageSync('userInfo').uid,
	  // uid:'da3bdf61cc1a409cafe573ea389c0e09'
    }).then(res => {
      // console.log(res,111)
      this.setData({
        balance: res || 0
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
    // 获取推荐会员数
    api.post(userProfit.teamNum, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res)
      this.setData({
        teamNum: res || 0
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
    this.getProfitList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	  var userInfo=wx.getStorageSync('userInfo')
	this.setData({
		uid: userInfo.uid,
		// role: wx.getStorageSync('userInfo').role
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		dataList: [],
		searchGood: {
			page: 1,
			size: 10,
		  pages: 0
		},
	})
  	this.onLoad()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
	if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
	this.setData({
	  searchGood: {
	    page: this.data.searchGood.page + 1,
	    size: 10,
	    pages: this.data.searchGood.pages
	  }
	})
	this.getProfitList()
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取收益记录列表
  getProfitList() {
    api.post(userProfit.list, {
      uid:this.data.uid|| wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res,'222')
      let list = res
      list.map(a => {
        a.recordTime = util.getLocalTime(a.recordTime)
        return a
      })
      this.setData({
        dataList: list
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  }
})